package com.erpanderp.wam.cm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.cm.model.entity.AdditionalSkill;
import com.erpanderp.wam.cm.model.entity.EmployeeSkillMatrix;
import com.erpanderp.wam.cm.model.entity.SAPCertification;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.util.HibernateUtil;

public class EmployeeSkillDAO {


	public EmployeeSkillMatrix saveEmployeeSkillMatrix(EmployeeSkillMatrix employeeSkillMatrix){
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.saveOrUpdate(employeeSkillMatrix);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
			
		}
		return employeeSkillMatrix;
	}

	
	public boolean saveSAPCertifications(List<SAPCertification> certificationsList){
		boolean certificationsCreated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			for(SAPCertification certification : certificationsList){
				session.saveOrUpdate(certification);
			}
			session.getTransaction().commit();
			certificationsCreated = true;
		}catch(Exception e){
			e.printStackTrace();
			return certificationsCreated;
			
		}
		return certificationsCreated;
	}	
	
	public boolean saveAdditionalSkills(List<AdditionalSkill> aditionalSkills){
		boolean aditionalSkillsCreated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			for(AdditionalSkill additionalSkill : aditionalSkills){
				session.saveOrUpdate(additionalSkill);
			}
			session.getTransaction().commit();
			aditionalSkillsCreated = true;
		}catch(Exception e){
			e.printStackTrace();
			return aditionalSkillsCreated;
			
		}
		return aditionalSkillsCreated;
	}
	
	public boolean isEmployeeAvailable(String employeeCode){
		boolean isEmployeeAvailable= false;
		System.out.println("employeeCode====DAO========"+employeeCode);
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.getTransaction().begin();
			
			String queryString = "from EmployeeSkillMatrix es where es.employee.employeeCode = :employeeCode";
			
			Query query = session.createQuery(queryString).setString("employeeCode", employeeCode);
			
			System.out.println("=============query.list())===========DAO======="+query.list());
			System.out.println("=============query.list().size()===========DAO======="+isEmployeeAvailable);
			if(query.list() == null || query.list().size() == 0){
				isEmployeeAvailable = false;
			}else{
				isEmployeeAvailable = true;
			}						
			session.close();
		}catch(Exception e){
			e.printStackTrace();
		}	
		System.out.println("=============isEmployeeAvailable===========DAO======="+isEmployeeAvailable);
		return isEmployeeAvailable;
	}
/*	public EmployeeSkillMatrix getEmployeeSkillMatrixByEmployeeId(Long employeeId)
	{
		EmployeeSkillMatrix employeeSkillMatrix = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			employeeSkillMatrix = (EmployeeSkillMatrix) session.load(EmployeeSkillMatrix.class, employeeId);
			session.close();
			return employeeSkillMatrix;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}*/
	 public EmployeeSkillMatrix getEmployeeSkillMatrixByEmployeeCode(String employeeCode){ 
		 EmployeeSkillMatrix employeeSkillMatrix ;		
		 System.out.println("=====employeeCode===DAO==========="+employeeCode);
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from EmployeeSkillMatrix es where es.employee.employeeCode = :employeeCode";
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setString("employeeCode", employeeCode);			
				employeeSkillMatrix = (EmployeeSkillMatrix) query.uniqueResult();
				session.getTransaction().commit();
				System.out.println("=====employeeSkillMatrix===DAO==========="+employeeSkillMatrix);
				return employeeSkillMatrix;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	
	public List<SAPCertification> getAllSAPCertificationsBySkillMatrixID(Long employeeSkillMatrixId) {
		List<SAPCertification> sapCertifications = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from SAPCertification sc where sc.employeeSkill.employeeSkillMatrixId = :employeeSkillMatrixId";
			Query query = session.createQuery(queryString).setLong("employeeSkillMatrixId",employeeSkillMatrixId);
			query.setCacheable(false);
			sapCertifications = (List<SAPCertification>) query.list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return sapCertifications;
	}
	public List<AdditionalSkill> getAllAdditionalSkillsBySkillMatrixID(Long employeeSkillMatrixId) {
		List<AdditionalSkill> additionalSkills = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from AdditionalSkill as ads where ads.employeeSkill.employeeSkillMatrixId = :employeeSkillMatrixId";
			Query query = session.createQuery(queryString).setLong("employeeSkillMatrixId",employeeSkillMatrixId);
			query.setCacheable(false);
			additionalSkills = (List<AdditionalSkill>) query.list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return additionalSkills;
	}
	
	/*public void deleteSAPCerificationById(Long sapCertificationId){
		System.out.println("INSIDE DAO :::::::"+sapCertificationId);
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from SAPCertification sc where sc.sapCertificationId=:sapCertificationId";
			Query query = session.createQuery(queryString).setLong("sapCertificationId",sapCertificationId);
			query.setCacheable(false);
			session.delete(query);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}*/
	
	public SAPCertification deleteSAPCerificationById(Long sapCertificationId)
	{
		SAPCertification sapCertification = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			sapCertification = (SAPCertification) session.load(SAPCertification.class, sapCertificationId);
			session.delete(sapCertification);
			session.getTransaction().commit();
			return sapCertification;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	public AdditionalSkill deleteAdditionalSkillById(Long additionalSkillId)
	{
		AdditionalSkill additioanlSkill = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			additioanlSkill = (AdditionalSkill) session.load(AdditionalSkill.class, additionalSkillId);
			session.delete(additioanlSkill);
			session.getTransaction().commit();
			return additioanlSkill;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	public List<EmployeeSkillMatrix> getAllEmployeeSkillsList() { 
		List<EmployeeSkillMatrix> allEmployeeSkills = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from EmployeeSkillMatrix es");
			query.setCacheable(false);
			allEmployeeSkills = (List<EmployeeSkillMatrix>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allEmployeeSkills;
	}
	public List<EmployeeSkillMatrix> getAllEmployeeSkillsByCompetency(String employeeCode) { 
		List<EmployeeSkillMatrix> allEmployeeSkills = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from EmployeeSkillMatrix es where es.employee.employeeCode in (select er.employee.employeeCode from EmployeeAndReporting er where er.competencelead.employeeCode =:employeeCode)");
			query.setCacheable(false).setString("employeeCode", employeeCode);
			allEmployeeSkills = (List<EmployeeSkillMatrix>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allEmployeeSkills;
	}
	public  static void main(String s[]){
		System.out.println(" List========="+new EmployeeSkillDAO().getAllAdditionalSkillsBySkillMatrixID(1l));
	}
		
}
